import { message } from 'antd';
import * as projectServices from '../services/project';


export default {
  namespace: 'project',
  state: {
    projectinfo: [],//数据库项目详情
    projectlist:[], //jira项目详情
  },
  reducers: { // 修改state
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    //查询数据库项目信息
    *fetchProject({ payload: { getparams } }, { call, put }) {
      const { data } = yield call(projectServices.fetchProject, getparams);// 接口调用
      const errorMes = data.msg || '查询失败';
      if (data.status !== 200) {
        message.error(errorMes);
      }
      yield put({
        type: 'save',
        payload: {
            projectinfo: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    *addProject({ payload: { param, cb } }, { call, put }) {
      const { data } = yield call(projectServices.addProject, param);
      const errorMes = data.msg || '新增失败';
      if (data.status === 200) {
        message.success('新增成功！');
        if (cb) cb();
        yield put({
          type: 'fetchProject',
          payload: {
            getparams: {
              name: '',
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *deleteProject({ payload: { param, cb } }, { call, put }) {
      const { data } = yield call(projectServices.deleteProject, param);
      const errorMes = data.msg || '删除失败';
      if (data.status === 200) {
        message.success('删除成功！');
        if (cb) cb();
        yield put({
          type: 'fetchProject',
          payload: {
            getparams: {
              name: '',
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *updateProject({ payload: { param, cb } }, { call, put }) {
      const { data } = yield call(projectServices.updateProject, param);
      const errorMes = data.msg || '修改失败';
      if (data.status === 200) {
        message.success('修改成功！');
        if (cb) cb();
        yield put({
          type: 'fetchProject',
          payload: {
            getparams: {
              name: '',
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    //获取jira的项目信息
    *fetchProjectList({ payload }, { call, put }) {
      const { data } = yield call(projectServices.getJiraProjectInfo);// 接口调用
      yield put({
        type: 'save',
        payload: {
          projectlist: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    //手动执行job，同步jira明细数据
    *executeJobs({ payload }, { call, put }) {
      const { data } = yield call(projectServices.executeJobs);// 接口调用
      const errorMes = data.msg || '同步失败';
      if (data.status === 200) {
        message.success('同步成功！');
      } else {
        message.error(errorMes);
      }
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/project') {
          dispatch({ type: 'fetchProjectList' });
          dispatch(
            { type: 'fetchProject',
              payload: {
                getparams: {
                  name: '',
                },
              } }
          );
        }
      });
    },
  },
};
